package com.blueberry.commbank.domain.entity;

import com.blueberry.commbank.exception.InvalidCurrencyException;
import java.math.BigDecimal;
import java.util.Currency;
import java.util.Objects;

public class ExchangeRate {
    private Currency source;
    private Currency target;
    private BigDecimal rate;

    public ExchangeRate(Currency source, Currency target, BigDecimal rate) {
        this.source = source;
        this.target = target;
        this.rate = rate;
    }

    public Money exchange(Money money) {
        Objects.requireNonNull(money, "Source money cannot be null");
        if (!money.getCurrency().equals(this.source))
            throw new InvalidCurrencyException("Currency does not match");
        return new Money(money.getAmount().multiply(rate), target);
    }

    public Currency getSource() {
        return source;
    }

    public ExchangeRate setSource(Currency source) {
        this.source = source;
        return this;
    }

    public Currency getTarget() {
        return target;
    }

    public ExchangeRate setTarget(Currency target) {
        this.target = target;
        return this;
    }

    public BigDecimal getRate() {
        return rate;
    }

    public ExchangeRate setRate(BigDecimal rate) {
        this.rate = rate;
        return this;
    }
}
